<script setup lang="ts">
import { getCategoryTopAPI } from '@/api/category';
import { getHomeBannerApi } from '@/api/home';
import type { CategoryTopItem } from '@/types/category';
import type { BannerItem } from '@/types/home';
import { onLoad, onShow } from '@dcloudio/uni-app';
import { computed } from 'vue';
import { ref } from 'vue';
import CategoryPageSkeleton from './components/CategoryPageSkeleton.vue'

// 获取轮播图数据
const bannerData = ref<BannerItem[]>([])
const getBannerData = async ()=>{
  const res = await getHomeBannerApi(2)
  bannerData.value = res.result
}

// 获取分类页左栏目分类选项
const categoryList = ref<CategoryTopItem[]>([])
const getCategoryData = async ()=>{
  const res = await getCategoryTopAPI()
  categoryList.value = res.result
}

// 侧边栏当前激活项
const activeIndex = ref(0)

// 提取二级分类（右侧商品栏）数据:(根据当前激活项的改变，而去改变右侧菜单栏数据)
const subCategoryList = computed(()=>{
  return categoryList.value[activeIndex.value]?.children || []
})

const isFinish = ref(false)
// 页面加载时
onLoad(async (option)=>{
  await Promise.all([getBannerData(), getCategoryData()])
  isFinish.value = true
})
onShow(()=>{
  // 获取缓存中的当前分类索引
  const curIndex = uni.getStorageSync('curIndex')
  if (curIndex) {
    activeIndex.value = curIndex
  }
})
</script>

<template>
  <view class="viewport" v-if="isFinish">
    <!-- 搜索框 -->
    <view class="search">
      <view class="input">
        <text class="icon-search">女靴</text>
      </view>
    </view>
    <!-- 分类 -->
    <view class="categories">
      <!-- 左侧：一级分类 -->
      <scroll-view class="primary" scroll-y>
        <view v-for="(item, index) in categoryList" :key="item.id" class="item" :class="{ active: index === activeIndex }" @tap="activeIndex = index">
          <text class="name"> {{item.name}} </text>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view class="secondary" scroll-y>
        <!-- 焦点图 -->
        <FnSwiper class="banner" :list="bannerData" />
        <!-- 内容区域 -->
        <view class="panel" v-for="item in subCategoryList" :key="item.id">
          <view class="title">
            <text class="name">{{item.name}}</text>
            <navigator class="more" hover-class="none">全部</navigator>
          </view>
          <view class="section">
            <navigator
              v-for="goods in item.goods"
              :key="goods.id"
              class="goods"
              hover-class="none"
              :url="`/pages/goods/goods?id=${goods.id}`"
            >
              <image
                class="image"
                :src="goods.picture"
              ></image>
              <view class="name ellipsis">{{goods.name}}</view>
              <view class="price">
                <text class="symbol">¥</text>
                <text class="number">{{goods.price}}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
  <CategoryPageSkeleton v-else/>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.search {
  padding: 0 30rpx 20rpx;
  background-color: #fff;
  .input {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64rpx;
    padding-left: 26rpx;
    color: #8b8b8b;
    font-size: 28rpx;
    border-radius: 32rpx;
    background-color: #f3f4f4;
  }
}
.icon-search {
  &::before {
    margin-right: 10rpx;
  }
}
/* 分类 */
.categories {
  flex: 1;
  min-height: 400rpx;
  display: flex;
}
/* 一级分类 */
.primary {
  overflow: hidden;
  overflow-y: scroll;
  width: 180rpx;
  flex: none;
  background-color: #f6f6f6;
  .item {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
  }
  .active {
    background-color: #fff;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 0;
      width: 8rpx;
      height: 100%;
      background-color: #db1818;
    }
  }
}
.primary .item:last-child::after,
.primary .active::after {
  display: none;
}
/* 二级分类 */
.secondary {
  background-color: #fff;
  .carousel {
    height: 200rpx;
    margin: 0 30rpx 20rpx;
    border-radius: 4rpx;
    overflow: hidden;
  }
  .panel {
    margin: 0 30rpx 0rpx;
  }
  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f7f7f8;
    .more {
      float: right;
      padding-left: 20rpx;
      font-size: 24rpx;
      color: #999;
    }
  }
  .more {
    &::after {
      font-family: 'erabbit' !important;
      content: '\e6c2';
    }
  }
  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;
    .goods {
      width: 140rpx;
      margin: 0rpx 30rpx 20rpx 0;
      &:nth-child(3n) {
        margin-right: 0;
      }
      image {
        width: 140rpx;
        height: 140rpx;
      }
      .name {
        padding: 5rpx;
        font-size: 22rpx;
        color: #333;
      }
      .price {
        padding: 5rpx;
        font-size: 18rpx;
        color: #cf4444;
      }
      .number {
        font-size: 24rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>